package com.xiyu.service.repository.market.task;

import com.xiyu.service.enums.market.task.MarketTaskStatusEnum;
import com.xiyu.service.model.market.setting.MarketSettingReplyFetcher;
import com.xiyu.service.model.market.setting.MarketSettingSenderFetcher;
import com.xiyu.service.model.market.task.*;
import com.xiyu.service.vo.market.marketTask.MarketTaskPageQueryInput;
import org.babyfish.jimmer.spring.repository.JRepository;
import org.babyfish.jimmer.Page;

import java.util.Optional;
import java.util.List;

public interface MarketTaskRepository extends JRepository<MarketTask, Long> {
    MarketTaskTable marketTaskTable = MarketTaskTable.$;

    default Page<MarketTask> pageQuery(MarketTaskPageQueryInput inputVO, List<Long> ownerIds){
        return sql().createQuery(marketTaskTable)
                .whereIf(inputVO.getCreatorId() != null, marketTaskTable.creatorId().eq(inputVO.getCreatorId()))
                .whereIf(inputVO.getCreateTime() != null, () -> marketTaskTable.createTime().between(inputVO.getCreateTime()[0], inputVO.getCreateTime()[1]))
                .where(marketTaskTable.creatorId().in(ownerIds))
                .orderBy(marketTaskTable.id().desc())
                .select(marketTaskTable.fetch(MarketTaskFetcher.$.allTableFields()
                        .sender(MarketSettingSenderFetcher.$.allTableFields())
                        .reply(MarketSettingReplyFetcher.$.allTableFields())))
                .fetchPage(inputVO.getPageNo() - 1, inputVO.getPageSize());
    }

    default Optional<MarketTask> findDetailById(Long id){
        return sql().createQuery(marketTaskTable)
                .where(marketTaskTable.id().eq(id))
                .select(marketTaskTable.fetch(MarketTaskFetcher.$.allTableFields()
                        .content(MarketTaskContentFetcher.$.allTableFields())
                        .receivers(MarketTaskReceiverFetcher.$.allTableFields())
                        .resources(MarketTaskResourceFetcher.$.allTableFields())
                ))
                .fetchOptional();
    }

    default List<Long> findScheduledTask(){
        return sql().createQuery(marketTaskTable)
                .where(marketTaskTable.status().eq(MarketTaskStatusEnum.UNEXECUTED.getValue()))
                .where(marketTaskTable.isScheduled().eq(true))
                .select(marketTaskTable.id())
                .execute();
    }

    default void updateStatus(Long id, Integer status){
        sql().createUpdate(marketTaskTable)
                .where(marketTaskTable.id().eq(id))
                .set(marketTaskTable.status(), status)
                .execute();
    }

    default void updateErrorMessage(Long id, String errorMessage){
        sql().createUpdate(marketTaskTable)
                .where(marketTaskTable.id().eq(id))
                .set(marketTaskTable.errorMessage(), errorMessage)
                .execute();
    }

    default void updateRequestCount(Long id, Integer requestCount){
        sql().createUpdate(marketTaskTable)
                .where(marketTaskTable.id().eq(id))
                .set(marketTaskTable.requestCount(), requestCount)
                .execute();
    }

    default void updateDeliverCount(Long id, Integer deliverCount){
        sql().createUpdate(marketTaskTable)
                .where(marketTaskTable.id().eq(id))
                .set(marketTaskTable.deliverCount(), deliverCount)
                .execute();
    }

    default void updateClickCount(Long id, Integer clickCount){
        sql().createUpdate(marketTaskTable)
                .where(marketTaskTable.id().eq(id))
                .set(marketTaskTable.clickCount(), clickCount)
                .execute();
    }

    default void updateOpenCount(Long id, Integer openCount){
        sql().createUpdate(marketTaskTable)
                .where(marketTaskTable.id().eq(id))
                .set(marketTaskTable.openCount(), openCount)
                .execute();
    }

    default void updateReplyCount(Long id, Integer replyCount){
        sql().createUpdate(marketTaskTable)
                .where(marketTaskTable.id().eq(id))
                .set(marketTaskTable.replyCount(), replyCount)
                .execute();
    }

    default void updateInvalidCount(Long id, Integer invalidCount){
        sql().createUpdate(marketTaskTable)
                .where(marketTaskTable.id().eq(id))
                .set(marketTaskTable.invalidCount(), invalidCount)
                .execute();
    }


}